//10．编写一个程序打开一个文本文件，通过交互方式获得文件名。
//通过一个循环，提示用户输入一个文件位置。
//然后该程序打印从该位置开始到下一个换行符之前的内容。
//用户输入负数或非数值字符可以结束输入循环。
#include <stdio.h>
#include <stdlib.h>

#define SIZE 256
int main(int size,char **argv){
    char filename[SIZE];
    long position ;
    int read_p;
    long last;
    int ch;
    FILE* file;
    printf("Enter the name of file:");
    //获取文件名
    while (scanf("%s", filename) != 1);
    if ((file = fopen(filename,"r"))==NULL){
        printf("Can't open %s", filename);
        exit(EXIT_FAILURE);
    }
    fseek(file,0,SEEK_END);
    last = ftell(file);
    printf("Enter the position of file:");
    read_p = scanf("%ld", &position);
    while (read_p == 1 && position >= 0) {

        if (position>=last){
            printf("last position of file is %ld\n", last);
        } else{
            fseek(file, position, SEEK_SET);
            printf("该位置到换行符的字符串是:");
            while ((ch = fgetc(file)) != '\n') {
                putchar(ch);
            }
            putchar('\n');
        }

        printf("Enter the next position of file:");
        read_p = scanf("%ld", &position);
    }
    return 0;
}